package com.dayuanit.dy15.ebook.bookshop.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    /**
     * 创建订单延迟队列
     * @return
     */
    @Bean
    public Queue delayOrderQueueTTL() {
      return  QueueBuilder.durable("delayOrderQueueTTL")
              .withArgument("x-dead-letter-exchange", "orderExchange")
              .withArgument("x-dead-letter-routing-key", "orderCloseRouting")
              .withArgument("x-message-ttl", 20000)
              .build();
    }

    /**
     * 要被关闭的订单队列
     * @return
     */
    @Bean
    public Queue orderCloseQueue() {
        return new Queue("orderCloseQueue");
    }

    /**
     * 订单的交换器
     * @return
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange("orderExchange");
    }


    @Bean
    public Binding bindOrderExchangeToOrderCloseQueue() {
        return BindingBuilder.bind(orderCloseQueue())
                .to(orderExchange())
                .with("orderCloseRouting");
    }

    @Bean
    public Binding bindOrderExchangeToDelayOrderQueueTTL() {
        return BindingBuilder.bind(delayOrderQueueTTL())
                .to(orderExchange())
                .with("delayOrderRouting");
    }

}
